//
//  AlgorithmPrepare.m
//  iBeaconDemo
//
//  Created by cry on 17/3/15.
//  Copyright © 2017年 eGova. All rights reserved.
//

#import "AlgorithmManager.h"
#import "TTBeaconRegion.h"
#import "AlgorithmHelper.h"
#import "TTReceiver.h"
@import CoreLocation;
@import CoreBluetooth;
/// 通通停车所有发射器uuid为同一个
static NSString *const UUID = @"975E500D-1DDD-4A5B-8CDA-F97433005858";

@interface AlgorithmManager()<CLLocationManagerDelegate, CBCentralManagerDelegate>

@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CLBeaconRegion *ttBeaconRegion;
@property (strong, nonatomic) CBCentralManager *centralManager;
@property (assign, nonatomic) BOOL isPrepare;
@property (strong, nonatomic) TTPoint *lastPoint;
@property (strong, nonatomic) NSMutableArray<TTReceiver *> *receivers;

@end

@implementation AlgorithmManager

+ (instancetype)manager{
    AlgorithmManager *ap = [[AlgorithmManager alloc] init];
    [ap prepare];
    return ap;
}

- (void)start{
    if (self.isWorking || self.isPrepare == NO) {
        return;
    }
    NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:UUID];
    if (self.ttBeaconRegion == nil) {
        self.ttBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"INDENTIFIER"];
    }
    if (self.receivers == nil) {
        [self getReceiversByServer];
    }else{
        [self startMonitoringItem:self.ttBeaconRegion];
    }
}

- (void)stop{
    if (self.ttBeaconRegion != nil) {
        [self stopMonitoringItem:self.ttBeaconRegion];
    }
}

- (void)prepare{
    
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    [self.locationManager requestWhenInUseAuthorization];
    // 蓝牙检测代理
    self.isPrepare = NO;
    self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
    //第一次打开或者每次蓝牙状态改变都会调用这个函数
    if(central.state == CBManagerStatePoweredOn){
        
        if ([CLLocationManager locationServicesEnabled]) {
            self.isPrepare = YES;
        }else{
            NSLog(@"没有打开定位！");
            self.isPrepare = NO;
        }
    }
    else{
        NSLog(@"没有打开蓝牙");
        self.isPrepare = NO;
    }
}

- (void)getReceiversByServer{
    // 网络请求获取receivers
    [self startMonitoringItem:self.ttBeaconRegion];
}

- (void)startMonitoringItem:(CLBeaconRegion *)ttBeaconRegion {
    
    [self.locationManager startMonitoringForRegion:ttBeaconRegion];
    [self.locationManager startRangingBeaconsInRegion:ttBeaconRegion];
    self.isWorking = YES;
}

- (void)stopMonitoringItem:(CLBeaconRegion *)ttBeaconRegion {
    
    [self.locationManager stopMonitoringForRegion:ttBeaconRegion];
    [self.locationManager stopRangingBeaconsInRegion:ttBeaconRegion];
    self.isWorking = NO;
}

#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error {
    NSLog(@"Failed monitoring region: %@", error);
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error {
    NSLog(@"Location manager failed: %@", error);
}

- (void)locationManager:(CLLocationManager *)manager
        didRangeBeacons:(NSArray *)beacons
               inRegion:(CLBeaconRegion *)region{
    if (beacons == 0) {
        return;
    }
    TTPoint *point = [AlgorithmHelper helpWithBeacons:beacons receivers:self.receivers];
    // 计算
    if (point) {
        self.lastPoint = point;
        if (self.delegate && [self.delegate respondsToSelector:@selector(algorithmManager:didUpdatePosition:)]) {
            [self.delegate algorithmManager:self didUpdatePosition:point];
        }
    }
}

@end
